package cn.stronglink.asset.module.patrol.service;

import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.toolkit.IdWorker;
import cn.stronglink.asset.model.Patrol;
import cn.stronglink.asset.model.PatrolPlanTime;
import cn.stronglink.asset.module.patrol.mapper.PatrolMapper;
import cn.stronglink.asset.module.patrol.mapper.PatrolPlanMapper;
import cn.stronglink.asset.module.patrol.vo.PatrolPlanSearchVo;
import cn.stronglink.asset.module.patrol.vo.PatrolPlanVo;
import cn.stronglink.suite.core.util.WebUtil;

@Service
@Transactional(rollbackFor = Exception.class)
public class PatrolPlanService {
	@Autowired
	private PatrolPlanMapper patrolPlanMapper;
	
	@Autowired
	private PatrolMapper patrolMapper;

	/**
	 * 添加巡檢計劃
	 * 
	 * @param patrolPlanVo
	 */
	public void addPatrolPlan(PatrolPlanVo patrolPlanVo) {
		if (patrolPlanVo.getId() == null) {
			patrolPlanVo.setType(1);
			patrolPlanVo.setId(IdWorker.getId());
			patrolPlanVo.setCreateBy(WebUtil.getCurrentUser());
			patrolPlanVo.setCreateTime(new Date());
		}
		patrolPlanMapper.addPatrolPlan(patrolPlanVo);

		if (patrolPlanVo.getPatrolPlanTimes() != null && patrolPlanVo.getPatrolPlanTimes().size() > 0) {
			for (PatrolPlanTime time : patrolPlanVo.getPatrolPlanTimes()) {
				time.setPatrolPlanId(patrolPlanVo.getId());
				time.setId(IdWorker.getId());
			}
			HashMap<String,Object> map=new HashMap<String,Object>();
			map.put("patrolPlanTimes", patrolPlanVo.getPatrolPlanTimes());
			patrolPlanMapper.addPatrolPlanTime(map);
		}

	}

	/**
	 * 更新巡检计划
	 * 
	 * @param patrolPlanVo
	 */
	public void updatePatrolPlan(PatrolPlanVo patrolPlanVo) {
		patrolPlanVo.setType(1);
		patrolPlanVo.setUpdateBy(WebUtil.getCurrentUser());
		patrolPlanVo.setUpdateTime(new Date());
		patrolPlanMapper.updatePatrolPlan(patrolPlanVo);
		patrolPlanMapper.deletePatrolPlanTime(patrolPlanVo.getId());
		if (patrolPlanVo.getPatrolPlanTimes() != null && patrolPlanVo.getPatrolPlanTimes().size() > 0) {
			for (PatrolPlanTime time : patrolPlanVo.getPatrolPlanTimes()) {
				time.setPatrolPlanId(patrolPlanVo.getId());
				time.setId(IdWorker.getId());
			}
			HashMap<String,Object> map=new HashMap<String,Object>();
			map.put("patrolPlanTimes", patrolPlanVo.getPatrolPlanTimes());
			patrolPlanMapper.addPatrolPlanTime(map);
		}
	}

	/**
	 * 删除巡检计划
	 * 
	 * @param patrolPlanIds
	 */
	public void delPatrolPlanById(Long id) {
		patrolPlanMapper.delPatrolPlanById(id);
		patrolPlanMapper.deletePatrolPlanTime(id);
	}

	/**
	 * 查詢計劃列表
	 * 
	 * @param page
	 * @param params
	 * @return
	 */
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public Page<PatrolPlanSearchVo> queryPatrolPlanList(Page<PatrolPlanSearchVo> page, Map<String, Object> params) {
		List<PatrolPlanSearchVo> patrolplanSearchVos = patrolPlanMapper.queryPatrolPlanList(page, params);
		if (patrolplanSearchVos != null && patrolplanSearchVos.size() > 0) {
			for (PatrolPlanSearchVo patrolPlanSearchVo : patrolplanSearchVos) {
				List<PatrolPlanTime> patrolPlanTimeList = patrolPlanMapper
						.getPatrolPlanTimes(patrolPlanSearchVo.getId());
				String patrolTime = "";
				String[] day1 = { "周一", "周二", "周三", "周四", "周五", "周六", "周日" };
				if (patrolPlanTimeList != null && patrolPlanTimeList.size() > 0) {
					for (int i = 0; i < patrolPlanTimeList.size(); i++) {
						PatrolPlanTime ppt = patrolPlanTimeList.get(i);
						if (patrolPlanSearchVo.getCycleType() == 2) {
							patrolTime += day1[ppt.getDay() - 1];
						} else if (patrolPlanSearchVo.getCycleType() == 3) {
							patrolTime += ppt.getDay() + "日";
						}
						patrolTime += ppt.getStartTime() + "-" + ppt.getEndTime();
						if (i != patrolPlanTimeList.size() - 1) {
							patrolTime += ";";
						}
					}
				}
				patrolPlanSearchVo.setPatrolPlanTime(patrolTime);
			}
		}
		page.setRecords(patrolplanSearchVos);
		return page;
	}
	
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public PatrolPlanVo qryById(Long id) {
		PatrolPlanVo vo=patrolPlanMapper.qryById(id);
		//获取时间
		List<PatrolPlanTime> timesList=patrolPlanMapper.getPotrolplanTime(id);
		vo.setPatrolPlanTimes(timesList);
		return vo;
	}

	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public void qryInspectToday() throws ParseException{
		// 获取今天所有的巡检计划
		int weekDay = 0;// 获取今天周几
		Calendar cal = Calendar.getInstance();
		if (cal.get(Calendar.DAY_OF_WEEK) == 1) {
			weekDay = 7;
		} else {
			weekDay = cal.get(Calendar.DAY_OF_WEEK) - 1;
		}
		int day = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);// 获取今天几号
		List<Patrol> list = patrolPlanMapper.qryInspectToday(weekDay, day);
		if (list != null && list.size() > 0) {
			for (Patrol p : list) {
				p.setId(IdWorker.getId());
				p.setInspectDate(new Date());
				p.setType(1);
				p.setStatus(1);
				p.setCreateTime(new Date());
				patrolMapper.insertInspection(p);
			}
		}
	}

	

}
